package com.example.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.StudentMapper;
import com.example.entity.Student;
import com.example.service.StudentService;

import java.util.List;

/**
 * Student Service 实现
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Value("${app.images.domain}") // 从配置文件application.properties中读取图片服务域名
    private String domain;

    /**
     * 处理单个学生的业务数据组装
     */
    public Student processStudentData(Student student) {
        if (student != null) {
            // 组装 avatarUrl
            if (StringUtils.hasText(student.getAvatarUrl()) && !student.getAvatarUrl().startsWith("http")) {
                student.setAvatarUrl("https://" + domain + "/" + student.getAvatarUrl());
            }
        }
        return student;
    }

    /**
     * 处理学生列表的业务数据组装
     */
    public List<Student> processStudentList(List<Student> students) {
        if (students != null && !students.isEmpty()) {
            students.forEach(this::processStudentData);
        }
        return students;
    }

    @Override
    public Student getById(java.io.Serializable id) {
        Student student = super.getById(id);
        return processStudentData(student);
    }

    @Override
    public List<Student> list() {
        List<Student> students = super.list();
        return processStudentList(students);
    }
}
